
import { defineStore } from 'pinia'



// 设置权限访问菜单
const setPermissionsArray = (permissions) => {
    let arr = []
    permissions.forEach((item) => {

        if (Object.keys(item)[0] === 'all') {
            arr.push('all')
        } else {
            arr.push(item.code)
        }

    })
    return arr
}


export const UserStore = defineStore('UserStore', {
    state: () => {
        return {
            username: "",
            user_id: '',
            auth_token: "",
            permissions: [],

        }
    },
    getters: {},


    actions: {
        setUserInfo(data) {
            const { username, user_id, auth_token, permissions } = data
            this.username = username
            this.user_id = user_id
            this.auth_token = auth_token
            this.permissions = setPermissionsArray(permissions);

        },

        clearUserInfo() {
            this.username = "",
                this.user_id = '',
                this.auth_token = '',
                this.permissions = []
        },

    },
    persist: true


})





